package com.mzba.zxdlyllp;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;

import com.mzba.zxdlyllp.staggeredgrid.StaggeredAdapter;
import com.mzba.zxdlyllp.utils.AppContext;
import com.mzba.zxdlyllp.utils.HttpUtils;
import com.mzba.zxdlyllp.utils.StringUtil;
import com.mzba.zxdlyllp.utils.UICore;
import com.mzba.zxdlyllp.widget.StaggeredGridView;
import com.mzba.zxdlyllp.widget.StaggeredGridView.OnItemClickListener;

/**
 * 
 * @author 06peng
 *
 */
public class WaterfallActivity extends BasicActivity implements Callback {
	
	private Handler handler;
	
	private String urls[];
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		handler = new Handler(this);
		setContentView(R.layout.activity_waterfall);
		UICore.eventTask(this, this, 0, null, true);
	}
	
	@Override
	public void execute(int mes, Object obj) {
		try {
			String result = HttpUtils.doGet(AppContext.load_images_url);
			if (StringUtil.isNotBlank(result)) {
				urls = result.split(",");
			}
			handler.sendEmptyMessage(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		StaggeredGridView gridView = (StaggeredGridView) this.findViewById(R.id.staggeredGridView1);
		int margin = getResources().getDimensionPixelSize(R.dimen.staggered_margin);
		gridView.setItemMargin(margin); // set the GridView margin
		gridView.setPadding(margin, 0, margin, 0); // have the margin on the sides as well 
		StaggeredAdapter adapter = new StaggeredAdapter(this, R.id.imageView1, urls);
		gridView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		gridView.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(StaggeredGridView parent, View view, int position, long id) {
//				String url = urls[position];
//				if (StringUtil.isNotBlank(url)) {
//					Intent intent = new Intent(WaterfallActivity.this, PhotoDetailActivity.class);
//					intent.putExtra("small_pic", "");
//					intent.putExtra("big_pic", url);
//					startActivity(intent);
//				}
				Intent intent = new Intent(WaterfallActivity.this, PhotosViewPagerActivity.class);
				intent.putExtra("position", position);
				intent.putExtra("urls", urls);
				startActivity(intent);
			}
		});
		return false;
	}
}
